import {createApp} from 'vue'
import App from './App.vue'
import {store} from './store'
import router from './router'
import {canTurnTo} from "./utils/RouterUtil";
import ArcoVue from '@arco-design/web-vue'
import '@arco-design/web-vue/dist/arco.css'
import 'font-awesome/css/font-awesome.css'
import {postRequest, putRequest, getRequest, deleteRequest, url} from './api/api'
import {downloadRequest} from "./utils/DownloadUtil";
import {initMenu} from './api/menus'
import {deepCopy, deepCopyWithJson} from './utils/ObjectUtil'
import moment from 'moment'

const app = createApp(App)
app.config.globalProperties.$post = postRequest
app.config.globalProperties.$put = putRequest
app.config.globalProperties.$get = getRequest
app.config.globalProperties.$delete = deleteRequest
app.config.globalProperties.$download = downloadRequest
app.config.globalProperties.requestUrl = url
app.config.globalProperties.$DeepCopy = deepCopy
app.config.globalProperties.$DeepCopyWithJson = deepCopyWithJson
app.config.globalProperties.$moment = moment
app.use(ArcoVue)
app.use(store)

router.beforeEach((to, from, next) => {
    if (window.localStorage.getItem("token")) {
        //初始化路由
        initMenu(router, store).then(res => {
            if (res) {
                let canTurn = canTurnTo(to.path, store.state.routes);
                //判断用户信息是否存在
                if (!window.localStorage.getItem("user")) {
                    return getRequest(url.getAdminInfo.url).then(res => {
                        if (res) {
                            //存入用户信息
                            window.localStorage.setItem("user", JSON.stringify(res.obj))
                            store.commit('initCurrentAdmin', res.obj)
                            //一些不存入数据库的固定页面,只要登录就能访问
                            if (to.path === '/'
                                || to.path === '/home'
                                || to.path === '/chat'
                                || to.path === '/admin/info'
                            ) {
                                next()
                            } else {
                                if (canTurn) {
                                    next()
                                } else {
                                    next('/home')
                                }
                            }
                        }
                    })
                } else {
                    store.commit('initCurrentAdmin', JSON.parse(window.localStorage.getItem('user')))
                }
                //一些不存入数据库的固定页面,只要登录就能访问
                if (to.path === '/'
                    || to.path === '/home'
                    || to.path === '/chat'
                    || to.path === '/admin/info'
                ) {
                    next()
                } else {
                    if (canTurn) {
                        next()
                    } else {
                        next('/home')
                    }
                }
            } else {
                next('/')
            }
        })
    } else {
        if (to.path === '/') {
            next()
        } else {
            next('/?redirect=' + to.path)
        }
    }
})
app.use(router)
app.mount('#app')
